package com.keqing.tomato.state;

import static android.widget.Toast.LENGTH_SHORT;
import static com.keqing.tomato.util.SoundUtil.palySound;
import static com.keqing.tomato.util.TimeUtil.getDateByOffset;

import android.widget.Toast;

import com.keqing.tomato.database.TomatoHistory;

import java.util.Calendar;
import java.util.Date;

public class FocusingState extends State {

    @Override
    public void initPage() {
        //do nothing
    }

    @Override
    public void clickBeginFocus() {
        //no this event
    }

    @Override
    public void clickBeginRest() {
        //no this event
    }

    @Override
    public void doTimerFinish() {
        fragment.timerFinishHandler.post(new Runnable() {
            @Override
            public void run() {
                if (fragment.mySetting.isAutoRest()) {
                    fragment.changeState(new RestingState());
                } else {
                    fragment.changeState(new BeginRestState());
                }
                fragment.timerFinishHandler.sendEmptyMessageDelayed(finish_focus, 1000);
                //振动
                playVibration();
                //响铃
                palySound(fragment.mySetting.getFocusSoundPath(), fragment.activity);
                //保存数据库
                Date now = new Date();
                TomatoHistory history = new TomatoHistory(
                        historyTitle(now),
                        fragment.mySetting.getFocusTime(),
                        getDateByOffset(now, Calendar.MINUTE, -fragment.mySetting.getFocusTime()),
                        now);
                history.save();
            }
        });
    }

    @Override
    public void clickReset() {
        Toast.makeText(fragment.activity, "请在专注结束后重置", LENGTH_SHORT).show();
    }

    @Override
    public void clickTimeMode() {
        fragment.timeModeBtn.setChecked(!fragment.timeModeBtn.isChecked());
        Toast.makeText(fragment.activity, "请在专注结束后设置", LENGTH_SHORT).show();
    }

}
